package name.fanxiong.restmgmt.user
{
	import com.adobe.serialization.json.JSON;
	
	import mx.controls.Alert;
	import mx.core.Container;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	public class User
	{
		/**
		 * 用户名
		 */
		public var username:String;
		
		/**
		 * 用户姓名
		 */
		public var name:String;
		private var password:String;
		
		/**
		 * 用户角色
		 */
		public var role:String;
		
		/**
		 * 用户权限
		 */
		public var power:Number;
		
		public var stackChild:String;
		
		/**
		 * 用户
		 * 
		 * @pare username:String 用户名
		 */
		public function User()
		{
			//TODO: implement function
		}
		
		public function UserLogin(username:String, password:String):void
		{
			var service:HTTPService = new HTTPService();
			service.url = "http://fanxiong.eicp.net/RestaurantPlatform-dev/php/userValidate.php";
			service.useProxy = false;
			service.method = "POST";
			service.resultFormat = "object";
			var param:Object = new Object();
			param['username'] = username;
			param['passwd']	= password;
			service.addEventListener(ResultEvent.RESULT, userLoginHandle);
			service.addEventListener(FaultEvent.FAULT, httpFault);
			service.send(param);
		}
		
		private function userLoginHandle(e:ResultEvent):void
		{
			var rawData:String = String(e.result);
			var json:Object = (JSON.decode(rawData) as Object);
			if(json.status == 1){
				//Alert.show(json.msg, "Congratulations!", Alert.OK);
				this.name		= json.name;
				this.role		= json.role;			
			}else{
				Alert.show(json.msg, "Error", Alert.OK);
			}
		}
		
		public function httpFault(e:FaultEvent):void
		{
			Alert.show(e.fault.message, e.type, Alert.OK);
		}

	}
}